/**
 * 
 */
package com.cqcis.uip.module.salary.oper.service.impl.course;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cqcis.uip.base.framework.utils.TimeUtil;
import com.cqcis.uip.module.salary.oper.SalaryConstants;
import com.cqcis.uip.module.salary.oper.dao.CourseRealRecordDao;
import com.cqcis.uip.module.salary.oper.domain.CourseRealRecord;
import com.cqcis.uip.module.salary.oper.domain.Employe;
import com.cqcis.uip.module.salary.oper.domain.SalaryCourseTimesDetail;
import com.cqcis.uip.module.salary.oper.domain.SalaryCourseTimesManagerDetail;
import com.cqcis.uip.module.salary.oper.domain.SalaryMain;
import com.cqcis.uip.module.salary.oper.service.CourseService;
import com.cqcis.uip.module.salary.oper.service.impl.ProcessServiceImpl;

/**
 * @author 李世军
 *
 * 创建于 2012-3-20 下午02:54:13
 */
public class CourseServiceImpl implements CourseService {
	private static Log log = LogFactory.getLog(ProcessServiceImpl.class);
	private CourseRealRecordDao courseRealRecordDao ;
	
	public void setCourseRealRecordDao(CourseRealRecordDao courseRealRecordDao) {
		this.courseRealRecordDao = courseRealRecordDao;
	}

	/* (non-Javadoc)
	 * @see com.cqcis.uip.module.salary.oper.service.CourseService#setSalary(com.cqcis.uip.module.salary.oper.domain.Employe, com.cqcis.uip.module.salary.oper.domain.SalaryMain, java.util.List)
	 */
	public void setSalary(Employe e, SalaryMain main, List<SalaryCourseTimesDetail> details, List<SalaryCourseTimesManagerDetail> salaryCourseTimesManagerDetails) {
		List<CourseRealRecord> records = courseRealRecordDao.find(e.getId()) ;
		if(records!=null && records.size()>0){
			float total = 0 ;
			for(CourseRealRecord record : records){
				SalaryCourseTimesDetail detail = new SalaryCourseTimesDetail() ;
				detail.setCompany_id(record.getCompany_id()) ;
				detail.setStudent_id(record.getStudent_id()) ;
				detail.setEmploye_id(e.getId()) ;
				detail.setDo_time(record.getCreate_time()) ;
				detail.setBegin_time(record.getBegin_time()) ;
				detail.setEnd_time(record.getEnd_time()) ;
				detail.setCourse_id(record.getCourse_id()) ;
				detail.setStudy_type(record.getStudy_type()) ;
				detail.setCourse_times(record.getCourse_hour()) ;
				detail.setStudent_manager_id(record.getStudent_manager_id()) ;
				detail.setPrice(0L) ;
				detail.setRatio(0L) ;
				detail.setGrade_id(record.getGrade_id()) ;
				details.add(detail) ;
				//total += record.getCourse_hour() ;// TODO 这里总课时要根据：学习方式 算
			}
			
			if(total>40){
				// 获取年级总课时
				Map<Integer, Integer> gidocs= new HashMap<Integer, Integer>() ;
				for(int i=1; i<=12; i++){
					gidocs.put(i, 0) ;
				}
				for(SalaryCourseTimesDetail d : details){
					if(d.getGrade_id()>=1 && d.getGrade_id()<=5){
						gidocs.put(1, gidocs.get(1)+d.getCourse_times()) ;
						gidocs.put(2, gidocs.get(2)+d.getCourse_times()) ;
						gidocs.put(3, gidocs.get(3)+d.getCourse_times()) ;
						gidocs.put(4, gidocs.get(4)+d.getCourse_times()) ;
						gidocs.put(5, gidocs.get(5)+d.getCourse_times()) ;
					}else if(d.getGrade_id()==6){
						gidocs.put(6, gidocs.get(6)+d.getCourse_times()) ;
					}else if(d.getGrade_id()==7 || d.getGrade_id()==8){
						gidocs.put(7, gidocs.get(7)+d.getCourse_times()) ;
						gidocs.put(8, gidocs.get(8)+d.getCourse_times()) ;
					}else if(d.getGrade_id()==9 || d.getGrade_id()==10){
						gidocs.put(9, gidocs.get(9)+d.getCourse_times()) ;
						gidocs.put(10, gidocs.get(10)+d.getCourse_times()) ;
					}else if(d.getGrade_id()==11){
						gidocs.put(11, gidocs.get(11)+d.getCourse_times()) ;
					}else if(d.getGrade_id()==12){
						gidocs.put(12, gidocs.get(12)+d.getCourse_times()) ;
					}
				}
				
				// 设置单价
				for(SalaryCourseTimesDetail d : details){
					d.setPrice(SalaryConstants.getPrice(d.getGrade_id(),gidocs.get(d.getGrade_id()))) ;
				}
				
				// 设置提成比例,及算提成
				float ratio = (total-40L)/total ;
				for(SalaryCourseTimesDetail d : details){
					d.setRatio(ratio) ;
					d.setFee(d.getCourse_times()*d.getPrice()*d.getRatio()) ;
				}
				
				// 获取总提成
				float totalFee = 0L ;
				for(SalaryCourseTimesDetail d : details){
					totalFee += d.getFee() ;
				}
				main.setCourse_fee(totalFee) ;
			}else{
				main.setCourse_fee(0L) ;
				main.setRemark(main.getRemark()+"|该员工未满40课时,课时提成0") ;
			}
		}else{
			SalaryCourseTimesDetail detail = new SalaryCourseTimesDetail() ;
			detail.setEmploye_id(e.getId()) ;
			detail.setDo_time(TimeUtil.getCurrentTimeStr("yyyy-MM-dd")) ;
			detail.setCourse_id(Integer.parseInt(SalaryConstants.COURSE_JT)) ;
			detail.setRemark("该员工无课时记录!") ;
			details.add(detail) ;
			
			main.setCourse_fee(0L) ;
			main.setRemark(main.getRemark()+"|该员工无课时记录!") ;
		}
		
		// 以上代码废弃,仅共financecontrol_salarycoursetimesdetail详情展现
		//////////////////////////全职教师课时提成///////////////////////////////////////////////
		if(SalaryConstants.OFFICE_29.equals(e.getOffice_id()) && SalaryConstants.EMPLOYE_STATE_NORMAL.equals(e.getState())){
			List<SalaryCourseTimesDetail> tCourseDetails1 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_1) ;
			List<SalaryCourseTimesDetail> tCourseDetails2 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_2) ;
			List<SalaryCourseTimesDetail> tCourseDetails3 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_3) ;
			List<SalaryCourseTimesDetail> tCourseDetails4 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_4) ;
			List<SalaryCourseTimesDetail> tCourseDetails5 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_5) ;
			// TODO 1对N 无法统计
			@SuppressWarnings("unused")
			List<SalaryCourseTimesDetail> tCourseDetailsN = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_N) ;
			float c1 = getTCourseTimes(tCourseDetails1) ;// 一对一课时
			//float ct = getTCourseTimes(tCourseDetails1)+getTCourseTimes(tCourseDetails2)+getTCourseTimes(tCourseDetails3)+getTCourseTimes(tCourseDetails4) ;// 总课时
			//float ratio = (ct-SalaryConstants.TACHER_COURSE_MUST)/ct  ;
			float ratio = 1F ;
			
			
			float tCourseFee = 0F ;
			if(c1>SalaryConstants.TACHER_COURSE_MUST){
				useCourse11(tCourseDetails1) ;// 消耗40课时
				for(SalaryCourseTimesDetail d : tCourseDetails1){
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST) * 1F * ratio ;
					d.setRemark(d.getRemark()+"|单价:"+SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST)) ;
					d.setRemark(d.getRemark()+"|实际课时数-"+d.getNew_course_times()) ;
					d.setRemark(d.getRemark()+"|年级-"+d.getGrade_id()) ;
					d.setRemark(d.getRemark()+"|原课时数-"+d.getCourse_times()) ;
					d.setFee(fee) ;
					//log.info("明细："+d.getRemark()) ;
					tCourseFee += fee ;
				}
				if("10".equals(e.getId())){
					//log.info("一对一") ;
					//log.info(tCourseDetails1) ;
					//log.info(tCourseFee) ;
				}
				
				
				for(SalaryCourseTimesDetail d : tCourseDetails2){
					d.setNew_course_times(d.getCourse_times()/2) ;
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST) * 1.2F  * ratio * 1.2F;
					d.setRemark(d.getRemark()+"|单价:"+SalaryConstants.getPrice(d.getGrade_id(),c1-SalaryConstants.TACHER_COURSE_MUST)) ;
					d.setRemark(d.getRemark()+"|实际课时数-"+d.getNew_course_times()) ;
					d.setFee(fee) ;
					tCourseFee += fee ;
				}
			
				
				for(SalaryCourseTimesDetail d : tCourseDetails3){
					d.setNew_course_times(d.getCourse_times()/3) ;
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST) * 1.3F * ratio* 1.3F;
					d.setRemark(d.getRemark()+"|单价:"+SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST)) ;
					d.setRemark(d.getRemark()+"|实际课时数-"+d.getNew_course_times()) ;
					d.setFee(fee) ;
					tCourseFee += fee ;
				}
				
				for(SalaryCourseTimesDetail d : tCourseDetails4){
					d.setNew_course_times(d.getCourse_times()/4) ;
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST) * 1.4F * ratio* 1.4F ;
					d.setRemark(d.getRemark()+"|单价:"+SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST)) ;
					d.setRemark(d.getRemark()+"|实际课时数-"+d.getNew_course_times()) ;
					d.setRemark(d.getRemark()+"|年级-"+d.getGrade_id()) ;
					d.setRemark(d.getRemark()+"|原课时数-"+d.getCourse_times()) ;
					//log.info("明细："+d.getRemark()) ;
					d.setFee(fee) ;
					tCourseFee += fee ;
				}
				if("10".equals(e.getId())){
					//log.info("一对四") ;
					//log.info(tCourseDetails1) ;
					//log.info(tCourseFee) ;
				}
				
				for(SalaryCourseTimesDetail d : tCourseDetails5){
					d.setNew_course_times(d.getCourse_times()/5) ;
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST) * 1.5F * ratio* 1.5F ;
					d.setRemark(d.getRemark()+"|单价:"+SalaryConstants.getPrice(d.getGrade_id(), c1-SalaryConstants.TACHER_COURSE_MUST)) ;
					d.setRemark(d.getRemark()+"|实际课时数-"+d.getNew_course_times()) ;
					d.setRemark(d.getRemark()+"|原课时数-"+d.getCourse_times()) ;
					d.setFee(fee) ;
					tCourseFee += fee ;
				}
				
			}else{
				
				// 全职教师,一对一课时 不满40课时时计算
				float needCourse = SalaryConstants.TACHER_COURSE_MUST-c1 ;
				useCourse1N(tCourseDetails2, tCourseDetails3, tCourseDetails4, tCourseDetails5, needCourse) ;
				
				for(SalaryCourseTimesDetail d : tCourseDetails2){
					//d.setNew_course_times(d.getCourse_times()/2) ;
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), d.getCourse_times()) * 1.2F  * ratio ;//* 1.2F;
					d.setFee(fee) ;
					tCourseFee += fee ;
//					if(e.getId().equals("10"))
//					log.info("22"+fee) ;
				}
				
				for(SalaryCourseTimesDetail d : tCourseDetails3){
					//d.setNew_course_times(d.getCourse_times()/3) ;
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), d.getCourse_times()) * 1.3F * ratio;//* 1.3F;
					d.setFee(fee) ;
					tCourseFee += fee ;
//					if(e.getId().equals("10"))
//					log.info("33"+fee) ;
				}
				
				for(SalaryCourseTimesDetail d : tCourseDetails4){
					//d.setNew_course_times(d.getCourse_times()/4) ;
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), d.getCourse_times()) * 1.4F * ratio;//* 1.4F ;
					//log.info(d.getNew_course_times() +"-----"+SalaryConstants.getPrice(d.getGrade_id(), d.getCourse_times())) ;
					d.setFee(fee) ;
					tCourseFee += fee ;
//					if(e.getId().equals("10"))
//					log.info("44"+fee) ;
				}
				
				for(SalaryCourseTimesDetail d : tCourseDetails5){
					//d.setNew_course_times(d.getCourse_times()/5) ;
					float fee = d.getNew_course_times() * SalaryConstants.getPrice(d.getGrade_id(), d.getCourse_times()) * 1.5F * ratio;//* 1.5F ;
					d.setFee(fee) ;
					tCourseFee += fee ;
//					if(e.getId().equals("10"))
//					log.info("55"+fee) ;
				}
				
			}
//			if(e.getId().equals("10"))
//			log.info("tt"+tCourseFee) ;
			main.setCourse_fee(tCourseFee) ;
			return ;
		}
		
		
		
		//////////////////////////兼职教师课时提成///////////////////////////////////////////////
		if(SalaryConstants.OFFICE_29.equals(e.getOffice_id()) && SalaryConstants.EMPLOYE_STATE_5.equals(e.getState())){
			List<SalaryCourseTimesDetail> tCourseDetails1 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_1) ;
			List<SalaryCourseTimesDetail> tCourseDetails2 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_2) ;
			List<SalaryCourseTimesDetail> tCourseDetails3 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_3) ;
			List<SalaryCourseTimesDetail> tCourseDetails4 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_4) ;
			List<SalaryCourseTimesDetail> tCourseDetails5 = courseRealRecordDao.getGradeCourseTimes(e.getId(), SalaryConstants.STUDY_TYPE_5) ;
			
			
			
			float tCourseFee = 0F ;
			for(SalaryCourseTimesDetail d : tCourseDetails1){
				d.setNew_course_times(d.getCourse_times()/1) ;
				float fee = d.getNew_course_times() * courseRealRecordDao.getJzTeacherPrice(e.getId(), d.getGrade_id()) * 1F  * 1F;
				d.setFee(fee) ;
				tCourseFee += fee ;
			}
//			if(e.getId().equals("154")){
//				log.info("///////兼职教师课时提成//////////") ;
//				log.info(tCourseDetails1) ;
//				log.info(tCourseFee) ;
//				log.info(courseRealRecordDao.getJzTeacherPrice(e.getId(), 5)) ;
//			}
			
			for(SalaryCourseTimesDetail d : tCourseDetails2){
				d.setNew_course_times(d.getCourse_times()/2) ;
				float fee = d.getNew_course_times() * courseRealRecordDao.getJzTeacherPrice(e.getId(), d.getGrade_id()) * 1.2F  * 1.2F;
				d.setFee(fee) ;
				tCourseFee += fee ;
			}
			
			for(SalaryCourseTimesDetail d : tCourseDetails3){
				d.setNew_course_times(d.getCourse_times()/3) ;
				float fee = d.getNew_course_times() * courseRealRecordDao.getJzTeacherPrice(e.getId(), d.getGrade_id()) * 1.3F  * 1.3F;
				d.setFee(fee) ;
				tCourseFee += fee ;
			}
			
			for(SalaryCourseTimesDetail d : tCourseDetails4){
				d.setNew_course_times(d.getCourse_times()/4) ;
				float fee = d.getNew_course_times() * courseRealRecordDao.getJzTeacherPrice(e.getId(), d.getGrade_id()) * 1.4F * 1.4F ;
				d.setFee(fee) ;
				tCourseFee += fee ;
			}
			
			for(SalaryCourseTimesDetail d : tCourseDetails5){
				d.setNew_course_times(d.getCourse_times()/5) ;
				float fee = d.getNew_course_times() * courseRealRecordDao.getJzTeacherPrice(e.getId(), d.getGrade_id()) * 1.5F * 1.5F;
				d.setFee(fee) ;
				tCourseFee += fee ;
			}
			
			main.setCourse_fee(tCourseFee) ;
			return ;
		}
		
		
		
		
		
		//////////////////////////学管师课时提成///////////////////////////////////////////////
		if(SalaryConstants.OFFICE_30.equals(e.getOffice_id())){
			processStudentManager(e, main, salaryCourseTimesManagerDetails) ;
		}
		
		//////////////////////////教学教务主管课时提成///////////////////////////////////////////////
		if(SalaryConstants.OFFICE_28.equals(e.getOffice_id())){
			processStudentManagerUp(e, main, salaryCourseTimesManagerDetails) ;
		}
		
	}

	
	
	public static void main(String[] args) {
		float t1 = 32-10 ;
		float t2 = 8 ;
		float ratio = 30F/40F ;
		System.out.println((t1*60 + t2*40));
		System.out.println(ratio);
		System.out.println((t1*60 + t2*40)*ratio);
	}
	
	/**
	 * 获取学习方式对应总课时
	 * @param details
	 * @return
	 */
	private float getTCourseTimes(List<SalaryCourseTimesDetail> details){
		float t = 0 ;
		for(SalaryCourseTimesDetail detail : details){
			t += detail.getCourse_times() ;
		}
		return t ;
	}
	
	/**
	 * 消耗40课时
	 * @param details
	 * @return
	 */
	private List<SalaryCourseTimesDetail> useCourse11(List<SalaryCourseTimesDetail> details){
		float t = getTCourseTimes(details) ;
		for(SalaryCourseTimesDetail detail : details){
			detail.setRatio(detail.getCourse_times()/t) ;
		}
		
		for(SalaryCourseTimesDetail detail : details){
			detail.setNew_course_times(detail.getCourse_times() - SalaryConstants.TACHER_COURSE_MUST*detail.getRatio()) ;// 原课时 - 40*原课时所占原总课时比例
		}
		return details ;
	}
	
	/**
	 * 各种学习方式消耗指定课时数
	 * @param details2	一对二
	 * @param details3	一对三
	 * @param details4	一对四
	 * @param details5	一对五
	 * @param useNeedCourse
	 * @return 
	 */
	private void useCourse1N(	List<SalaryCourseTimesDetail> details2, 
														List<SalaryCourseTimesDetail> details3,
														List<SalaryCourseTimesDetail> details4,
														List<SalaryCourseTimesDetail> details5,
														float useNeedCourse){
		// 一对N课时总数
		float t = 0F ;
		
		// 转换一对N 课时数为一对一课时数
		if(details2!=null && details2.size()>0){
			for(SalaryCourseTimesDetail c : details2){
				c.setNew_course_times(c.getCourse_times()/2F*1.2F) ;
				t += c.getNew_course_times() ;
			}
		}
		if(details3!=null && details3.size()>0){
			for(SalaryCourseTimesDetail c : details3){
				c.setNew_course_times(c.getCourse_times()/3F*1.3F) ;
				t += c.getNew_course_times() ;
			}
		}
		if(details4!=null && details4.size()>0){
			for(SalaryCourseTimesDetail c : details4){
				c.setNew_course_times(c.getCourse_times()/4F*1.4F) ;
				t += c.getNew_course_times() ;
			}
		}
		if(details5!=null && details5.size()>0){
			for(SalaryCourseTimesDetail c : details5){
				c.setNew_course_times(c.getCourse_times()/5F*1.5F) ;
				t += c.getNew_course_times() ;
			}
		}
		
		
		// 减除比例
		if(details2!=null && details2.size()>0){
			for(SalaryCourseTimesDetail c : details2){
				float ratio = c.getNew_course_times()/t ;
				c.setNew_course_times(c.getNew_course_times() - useNeedCourse*ratio) ;
			}
		}
		if(details3!=null && details3.size()>0){
			for(SalaryCourseTimesDetail c : details3){
				float ratio = c.getNew_course_times()/t ;
				c.setNew_course_times(c.getNew_course_times() - useNeedCourse*ratio) ;
			}
		}
		if(details4!=null && details4.size()>0){
			for(SalaryCourseTimesDetail c : details4){
				float ratio = c.getNew_course_times()/t ;
				c.setNew_course_times(c.getNew_course_times() - useNeedCourse*ratio) ;
			}
		}
		if(details5!=null && details5.size()>0){
			for(SalaryCourseTimesDetail c : details5){
				float ratio = c.getNew_course_times()/t ;
				c.setNew_course_times(c.getNew_course_times() - useNeedCourse*ratio) ;
			}
		}

	}
	
	public static void main1(String[] args) {
		float a = 2 ;
		float b =52 ;
		System.out.println(a/b);
	}
	
	/**
	 * 学管师课时提成
	 * @param e
	 * @param main
	 * @param details
	 */
	private void processStudentManager(Employe e, SalaryMain main, List<SalaryCourseTimesManagerDetail> salaryCourseTimesManagerDetails){
		List<SalaryCourseTimesDetail> details = new ArrayList<SalaryCourseTimesDetail>() ;
		List<CourseRealRecord> records = courseRealRecordDao.findStudnetManager(e.getId()) ;
		if(records!=null && records.size()>0){
			for(CourseRealRecord record : records){
				SalaryCourseTimesDetail detail = new SalaryCourseTimesDetail() ;
				detail.setCompany_id(record.getCompany_id()) ;
				detail.setStudent_id(record.getStudent_id()) ;
				detail.setEmploye_id(e.getId()) ;
				detail.setDo_time(record.getCreate_time()) ;
				detail.setBegin_time(record.getBegin_time()) ;
				detail.setEnd_time(record.getEnd_time()) ;
				detail.setCourse_id(record.getCourse_id()) ;
				detail.setStudy_type(record.getStudy_type()) ;
				detail.setCourse_times(record.getCourse_hour()) ;
				detail.setStudent_manager_id(record.getStudent_manager_id()) ;
				detail.setPrice(0L) ;
				detail.setRatio(0L) ;
				detail.setGrade_id(record.getGrade_id()) ;
				detail.setCreate_time(record.getCreate_time()) ;
				details.add(detail) ;
			}
		}
		// 根据课时明细来计算 - financecontrol_salarycoursetimesdetail
		
		float taskCourse = courseRealRecordDao.getStudentManagerTask(e.getCompany_id()) ;
		float totalCourse = 0F ;
		float totalPrice = 0F ;
		float fee = 0F ;
		
		
		for(SalaryCourseTimesDetail detail : details){
			if(e.getId().equals(detail.getStudent_manager_id()+"")){
				// 总课时
				totalCourse += detail.getCourse_times() ;
				// 总价
				totalPrice += courseRealRecordDao.getCoursePrice(detail.getStudent_id(),detail.getCreate_time(), detail.getGrade_id(), detail.getCourse_id()) * Float.parseFloat(detail.getCourse_times()+"")  ;
			}
		}
		
		// 达到提成比例  按满额提成
		if( totalCourse>=taskCourse ){
			for(SalaryCourseTimesDetail detail : details){
				float currentPrice = courseRealRecordDao.getCoursePrice(detail.getStudent_id(),detail.getCreate_time(), detail.getGrade_id(), detail.getCourse_id()) ;
				float studentManagerFee = 0.1F*currentPrice*Float.parseFloat(detail.getCourse_times()+"")*1F ;
				detail.setStudent_manager_fee(studentManagerFee) ;
				fee += studentManagerFee ;
				
				SalaryCourseTimesManagerDetail managerDetail = new SalaryCourseTimesManagerDetail() ;
				managerDetail.setEmploye_id(e.getId()) ;
				managerDetail.setExpect_course(taskCourse) ;
				managerDetail.setReal_course(totalCourse) ;
				managerDetail.setReal_course_price(totalPrice) ;
				managerDetail.setRatio(1F) ;
				managerDetail.setStudent_id(detail.getStudent_id()) ;
				managerDetail.setGrade_id(detail.getGrade_id()) ;
				managerDetail.setCourse_id(detail.getCourse_id()) ;
				managerDetail.setCourse_times(detail.getCourse_times()) ;
				managerDetail.setPrice(currentPrice) ;
				managerDetail.setFee(studentManagerFee) ;
				salaryCourseTimesManagerDetails.add(managerDetail) ;
			}

			// 设置明细中的总价
			for(SalaryCourseTimesManagerDetail d : salaryCourseTimesManagerDetails){
				d.setReal_fee(fee) ;
			}
			
			main.setCourse_fee(fee) ;
			return ;
		}
		
		// 达到提成比例  按实际比例提成
		if( totalCourse>=(taskCourse*0.7) ){
			float nm = totalCourse/taskCourse ;
			for(SalaryCourseTimesDetail detail : details){
				float currentPrice = courseRealRecordDao.getCoursePrice(detail.getStudent_id(),detail.getCreate_time(), detail.getGrade_id(), detail.getCourse_id()) ;
				float studentManagerFee = 0.1F*currentPrice*Float.parseFloat(detail.getCourse_times()+"")*nm ;
				detail.setStudent_manager_fee(studentManagerFee) ;
				fee += studentManagerFee ;
				
				SalaryCourseTimesManagerDetail managerDetail = new SalaryCourseTimesManagerDetail() ;
				managerDetail.setEmploye_id(e.getId()) ;
				managerDetail.setExpect_course(taskCourse) ;
				managerDetail.setReal_course(totalCourse) ;
				managerDetail.setReal_course_price(totalPrice) ;
				managerDetail.setRatio(1F) ;
				managerDetail.setStudent_id(detail.getStudent_id()) ;
				managerDetail.setGrade_id(detail.getGrade_id()) ;
				managerDetail.setCourse_id(detail.getCourse_id()) ;
				managerDetail.setCourse_times(detail.getCourse_times()) ;
				managerDetail.setPrice(currentPrice) ;
				managerDetail.setFee(studentManagerFee) ;
				salaryCourseTimesManagerDetails.add(managerDetail) ;
			}
		}
		
		// 设置明细中的总价
		for(SalaryCourseTimesManagerDetail d : salaryCourseTimesManagerDetails){
			d.setReal_fee(fee) ;
		}
		
		main.setCourse_fee(fee) ;
	}
	
	
	/**
	 * 教学教务主管课时提成
	 * @param e
	 * @param main
	 * @param details
	 */
	private void processStudentManagerUp(Employe e, SalaryMain main, List<SalaryCourseTimesManagerDetail> salaryCourseTimesManagerDetails){
		List<SalaryCourseTimesDetail> details = new ArrayList<SalaryCourseTimesDetail>() ;
		// 主管课时明细
		List<CourseRealRecord> records = courseRealRecordDao.findStudnetManagerUp(e.getId()) ;
		if(records!=null && records.size()>0){
			for(CourseRealRecord record : records){
				SalaryCourseTimesDetail detail = new SalaryCourseTimesDetail() ;
				detail.setCompany_id(record.getCompany_id()) ;
				detail.setStudent_id(record.getStudent_id()) ;
				detail.setEmploye_id(e.getId()) ;
				detail.setDo_time(record.getCreate_time()) ;
				detail.setBegin_time(record.getBegin_time()) ;
				detail.setEnd_time(record.getEnd_time()) ;
				detail.setCourse_id(record.getCourse_id()) ;
				detail.setStudy_type(record.getStudy_type()) ;
				detail.setCourse_times(record.getCourse_hour()) ;
				detail.setStudent_manager_id(record.getStudent_manager_id()) ;
				detail.setPrice(0L) ;
				detail.setRatio(0L) ;
				detail.setGrade_id(record.getGrade_id()) ;
				details.add(detail) ;
			}
		}
		
		// 主管下面的学管师课时明细
		List<String> sms = courseRealRecordDao.findDownStudentManager(e.getCompany_id()) ;
		if(sms!=null && sms.size()>0)
		for(String smId : sms){
			records = courseRealRecordDao.findStudnetManager(smId) ;
			if(records!=null && records.size()>0){
				for(CourseRealRecord record : records){
					SalaryCourseTimesDetail detail = new SalaryCourseTimesDetail() ;
					detail.setCompany_id(record.getCompany_id()) ;
					detail.setStudent_id(record.getStudent_id()) ;
					detail.setEmploye_id(e.getId()) ;
					detail.setDo_time(record.getCreate_time()) ;
					detail.setBegin_time(record.getBegin_time()) ;
					detail.setEnd_time(record.getEnd_time()) ;
					detail.setCourse_id(record.getCourse_id()) ;
					detail.setStudy_type(record.getStudy_type()) ;
					detail.setCourse_times(record.getCourse_hour()) ;
					detail.setStudent_manager_id(record.getStudent_manager_id()) ;
					detail.setPrice(0L) ;
					detail.setRatio(0L) ;
					detail.setGrade_id(record.getGrade_id()) ;
					details.add(detail) ;
				}
			}
		}
		
		
		
		// 根据课时明细来计算 - financecontrol_salarycoursetimesdetail
		float taskCourse = courseRealRecordDao.getStudentManagerUpTask(e.getCompany_id()) ;
		float totalCourse = 0F ;
		float totalPrice = 0F ;
		float fee = 0F ;
		
		
		for(SalaryCourseTimesDetail detail : details){
			if(e.getId().equals(detail.getStudent_manager_id()+"")){
				// 总课时
				totalCourse += detail.getCourse_times() ;
				// 总价
				totalPrice += courseRealRecordDao.getCoursePrice(detail.getStudent_id(), detail.getCreate_time(),detail.getGrade_id(), detail.getCourse_id()) * Float.parseFloat(detail.getCourse_times()+"")  ;
			}
		}
		
		// 达到提成比例  按满额提成
		if( totalCourse>=taskCourse ){
			for(SalaryCourseTimesDetail detail : details){
				float currentPrice = courseRealRecordDao.getCoursePrice(detail.getStudent_id(),detail.getCreate_time(), detail.getGrade_id(), detail.getCourse_id()) ;
				float studentManagerFee = 0.06F*currentPrice*Float.parseFloat(detail.getCourse_times()+"")*1F ;
				detail.setStudent_manager_fee(studentManagerFee) ;
				fee += studentManagerFee ;
				
				SalaryCourseTimesManagerDetail managerDetail = new SalaryCourseTimesManagerDetail() ;
				managerDetail.setEmploye_id(e.getId()) ;
				managerDetail.setExpect_course(taskCourse) ;
				managerDetail.setReal_course(totalCourse) ;
				managerDetail.setReal_course_price(totalPrice) ;
				managerDetail.setRatio(1F) ;
				managerDetail.setStudent_id(detail.getStudent_id()) ;
				managerDetail.setGrade_id(detail.getGrade_id()) ;
				managerDetail.setCourse_id(detail.getCourse_id()) ;
				managerDetail.setCourse_times(detail.getCourse_times()) ;
				managerDetail.setPrice(currentPrice) ;
				managerDetail.setFee(studentManagerFee) ;
				salaryCourseTimesManagerDetails.add(managerDetail) ;
			}
			
			// 设置明细中的总价
			for(SalaryCourseTimesManagerDetail d : salaryCourseTimesManagerDetails){
				d.setReal_fee(fee) ;
			}
			
			main.setCourse_fee(fee) ;
			return ;
		}
		
		// 达到提成比例  按实际比例提成
		if( totalCourse>=(taskCourse*0.7) ){
			float nm = totalCourse/taskCourse ;
			for(SalaryCourseTimesDetail detail : details){
				float currentPrice = courseRealRecordDao.getCoursePrice(detail.getStudent_id(),detail.getCreate_time(), detail.getGrade_id(), detail.getCourse_id()) ;
				float studentManagerFee = 0.06F*currentPrice*Float.parseFloat(detail.getCourse_times()+"")*nm ;
				detail.setStudent_manager_fee(studentManagerFee) ;
				fee += studentManagerFee ;
				
				SalaryCourseTimesManagerDetail managerDetail = new SalaryCourseTimesManagerDetail() ;
				managerDetail.setEmploye_id(e.getId()) ;
				managerDetail.setExpect_course(taskCourse) ;
				managerDetail.setReal_course(totalCourse) ;
				managerDetail.setReal_course_price(totalPrice) ;
				managerDetail.setRatio(1F) ;
				managerDetail.setStudent_id(detail.getStudent_id()) ;
				managerDetail.setGrade_id(detail.getGrade_id()) ;
				managerDetail.setCourse_id(detail.getCourse_id()) ;
				managerDetail.setCourse_times(detail.getCourse_times()) ;
				managerDetail.setPrice(currentPrice) ;
				managerDetail.setFee(studentManagerFee) ;
				salaryCourseTimesManagerDetails.add(managerDetail) ;
			}
		}
		
		// 设置明细中的总价
		for(SalaryCourseTimesManagerDetail d : salaryCourseTimesManagerDetails){
			d.setReal_fee(fee) ;
		}
		
		main.setCourse_fee(fee) ;
	}
	
}
